package com.ebelter.btlibrary.btble.impl.ichoice;


import com.ebelter.btlibrary.btble.ble.bluetooth.device.impl.BleMessageAnalyser;
import com.ebelter.btlibrary.btble.ble.bluetooth.device.impl.Channel;
import com.ebelter.btlibrary.btble.impl.ecg.callback.EcgResultCallback;
import com.ebelter.btlibrary.btble.impl.ecg.model.EcgResult;
import com.ebelter.btlibrary.btble.impl.ichoice.callback.IChoiceResultCallback;
import com.ebelter.btlibrary.btble.impl.ichoice.model.IChoiceResult;
import com.ebelter.btlibrary.util.ULog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 血氧仪数据解析器
 * Created by Sergio Pan on 2017/12/8.
 */

public class IChoiceMessageAnalyser extends BleMessageAnalyser {

    private static final String TAG = "IChoiceMessageAnalyser";

    private static IChoiceMessageAnalyser instance = new IChoiceMessageAnalyser();

    //数据解析后的各种回调
    private IChoiceResultCallback measureResultCallback;

    private IChoiceMessageAnalyser() {
//        recordSet.
    }


    public static IChoiceMessageAnalyser getInstance() {
        return instance;
    }


    public void registerMeasureResultCallback(IChoiceResultCallback callback) {
        this.measureResultCallback = callback;
    }


    /**
     * 解密数据
     *
     * @param data
     * @return
     */
    @Override
    protected byte[] decrypt(byte[] data) {
//        ULog.i(TAG, "-------decrypt--------data = " + getArrayString(data));
        //TODO 如若是加密数据，在此做解密处理,如若非加密数据，可不重写此方法

        return data;
    }

    /**
     * 开始解析
     *
     * @param data
     * @return
     */
    @Override
    protected void analyze(Channel channel, byte[] data) {
        ULog.i(TAG, "-------analyze----before filter----data = " + getArrayString(data));
        //解密后
        int cmdCode0 = data[0] & 0xff;
        int cmdCode1 = data[1] & 0xff;
        if (cmdCode0 != 0x55 || cmdCode1 != 0xaa) {
            return;
        }
        //检验数据
        if (!checkData(data)) {
            return;
        }
        ULog.i(TAG, "-------analyze---after filter-----data = " + getArrayString(data));
        //业务码

        doResult(data);


    }


    /**
     * 秤唤醒
     */
    private void doResult(byte[] data) {

        int bloodOxygen = data[3];
        int pulse = data[4];
        //正在测量
        if (pulse == 0) {
            if (measureResultCallback != null) {
                measureResultCallback.onMeasuring(bloodOxygen);
            }
            return;
        }

        IChoiceResult result = new IChoiceResult();

        result.setBloodOxygen(bloodOxygen);
        result.setPulse(pulse);

        long measureTime = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date(measureTime);
        String measureTimeStr = sdf.format(curDate);

        result.setMeasureTime(measureTime);
        result.setMeasureTimeStr(measureTimeStr);

        //测量结果回调
        if (measureResultCallback != null) {
            measureResultCallback.onReceiveResult(result);
        }

    }

    private synchronized boolean checkData(byte[] data) {
        boolean isRealData = true;
        byte sum = 0;
        int count = data.length - 1;
        for (int i = 2; i < count; i++) {
            sum += data[i];
        }
        sum -= data[count];
        if (sum != 0) {
            isRealData = false;
        }
        return isRealData;
    }


    public int getDecryptValue(byte a, byte b) {
        int aa = a & 0xff;
        int bb = b & 0xff;
        return (bb << 8) | aa;
    }


    @Override
    public void onRelease() {
        super.onRelease();


    }
}
